package com.example.demo.controller;

import com.example.demo.entity.GoodsItem;
import com.example.demo.service.GoodsManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;
@Api("商品管理")
@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired()
    GoodsManageService service;

    @ApiOperation("根据编号查找商品")
    @GetMapping("/{id}")
    public ResponseEntity<GoodsItem> getGood(@PathVariable long id){
        GoodsItem good=service.getGood(id);
        if(good==null){
            return ResponseEntity.noContent().build();
        }
        else{
            return ResponseEntity.ok(good);
        }
    }

    @ApiOperation("根据商品名查找商品")
    @GetMapping("")
    public ResponseEntity<List<GoodsItem>> findGoods(String name){
        List<GoodsItem> result=service.findGoods(name);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("添加商品")
    @PostMapping("")
    public ResponseEntity<GoodsItem> addGood(@RequestBody GoodsItem good){
        GoodsItem result=service.addGood(good);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("修改商品")
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateGood(@PathVariable long id,@RequestBody GoodsItem good){
        service.updateGood(id,good);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteGood(@PathVariable long id){
        service.removeGood(id);
        return ResponseEntity.ok().build();
    }

}

